import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:plugin_ocr/bean/ocr_results.dart';
import 'package:plugin_ocr/plugin_ocr.dart';
import 'package:plugin_ocr/bean/ocr_result.dart';

import 'package:flutter/services.dart' show rootBundle;

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _ocrResultStr = '无';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await PluginOcr.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Container(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              FlatButton(
                  onPressed: () async {
                    OcrResultInfo ocrResultInfo = await PluginOcr.ocr;
                    print(ocrResultInfo.ocrResults.toString());
                    setState(() {
                      _ocrResultStr = ocrResultInfo.ocr_type +
                          '\n\n' +
                          (ocrResultInfo.ocr_type == 'EMPTY' ? '空白页' : '');
                      if (ocrResultInfo.ocr_type == 'BANK_CARD' &&
                          ocrResultInfo.ocrResults.length == 0) {
                        _ocrResultStr += '请重新识别';
                      }
                    });
                    ocrResultInfo.ocrResults.forEach((ocrResult) {
                      setState(() {
                        _ocrResultStr +=
                            '${ocrResult.index} ${ocrResult.name} ${ocrResult.confidence}\n ${ocrResult.bounds.toString()}\n\n';
                      });
                    });
                  },
                  child: Text('ocr')),
              FlatButton(
                onPressed: () async {
                  final imageByteData = await rootBundle.load('images/id3.jpg');
                  Directory dir = await getExternalStorageDirectory();
                  String path = dir.path;
                  File imageFile = File('$path/1.jpg');
                  imageFile.writeAsBytesSync(imageByteData.buffer.asInt8List(0));
                  OcrResultInfo ocrStr = await PluginOcr.ocrFromImage(imageFile.path,ocr_type: 'ID_CARD');
                  print(ocrStr.ocrResults.toString());
                },
                child: Text('ocrFromImage'),
              ),
              Expanded(child: SingleChildScrollView(child: Text(_ocrResultStr)))
            ],
          ),
        ),
      ),
    );
  }
}
